package com.flower.wing.mainserver;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;

public class LoginDisplay extends JFrame implements ActionListener {
	Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();

	public JTextField hostField = new JTextField("");
	public JTextField userField = new JTextField("");
	public JPasswordField pwdField= new JPasswordField("");
	
	private MainDisplay mainDisplay;
	private JFrame jfr;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private String loginSQL = null;
	private Properties info;
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	private int x, y;
	private File file = null;
	
	public LoginDisplay() {
		super("DB Management - developed by FlowerTeacher in WingFl.inc");		
		JPanel p1 = new JPanel(new GridLayout(2,1));
		p1.add(new JLabel("*경고*", SwingConstants.CENTER));
		p1.add(new JLabel("관계자는 출입금지", SwingConstants.CENTER));
        add(p1, BorderLayout.NORTH);
        
        JPanel p2 = new JPanel(new GridLayout(2,1,5,3));
		p2.add(new JLabel("   Enter ID  :   "));
		p2.add(new JLabel("   Enter PW  :   "));
		add(p2, BorderLayout.WEST);
        
        JPanel p3 = new JPanel(new GridLayout(2,1,5,3));
		p3.add(userField);
		p3.add(pwdField);
		pwdField.addActionListener(this);
		add(p3, BorderLayout.CENTER);
		
		JPanel p4 = new JPanel();
		okButton = addButton(p4, "Ok");
		cancelButton = addButton(p4, "Cancel");
		add(p4, BorderLayout.SOUTH);
		setSize(360, 150);
		x = screenSize.width / 2 - this.getWidth() / 2;  
        y = screenSize.height / 2 - this.getHeight() / 2;  
        setLocation(x, y);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	private JButton addButton(Container c, String name) {
		JButton button = new JButton(name);
		button.addActionListener(this);
		c.add(button);
		return button;
	}

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		if (source == okButton || source == pwdField) {
//			mainDisplay = new MainDisplay();
//			mainDisplay.setVisible(true);
//			this.dispose();
			try {
				this.dbConnect();
				this.loginServer();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (source == cancelButton){
			System.exit(0);
		}
	}
	
	public void dbConnect() throws Exception{
//		String id = "waviss";
//		String pwd = "waviss";
//		String url = "jdbc:oracle:thin:@163.0.1.205:1521/waviss";
//		this.conn = DriverManager.getConnection(url, id, pwd);
		this.file = new File("./info/Info.properties");
		this.info = new Properties();
		this.info.load(new FileInputStream(file));
		Class.forName(this.info.getProperty("DBDRIVER"));
		this.conn = DriverManager.getConnection(this.info.getProperty("DBURL"),
				this.info.getProperty("DBID"), this.info.getProperty("DBPWD"));
	}
	
	public void getStatement() throws SQLException{
		this.stmt = conn.createStatement();
	}
	
	private void executeSQL(String sql) throws SQLException {
		this.rs = this.stmt.executeQuery(sql);
	}
	
	private void loginServer(){
		EgovPasswordEncoder encoder = new EgovPasswordEncoder();
		
		String id = this.userField.getText().trim();
//		String pwd = encoder.encryptPassword(this.pwdField.getText().trim());
		String pwd = this.pwdField.getText().trim();
		
		String userID = null, userPWD = null;
		this.loginSQL = "SELECT USER_INFO.USER_ID, USER_INFO.USER_PW " +
				"FROM USER_INFO WHERE USER_INFO.USER_ID = '" + id + "'";
		
		try {
			this.getStatement();
			this.executeSQL(loginSQL);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (rs != null){
			try {
				while (rs.next()){
					userID = rs.getString(1);
					userPWD = rs.getString(2);
				}
//				System.out.println(userID + " <<-- realID");
//				System.out.println(userPWD + " <<-- realPWD");
//				System.out.println(id + " <<-- inputID");
//				System.out.println(pwd + " <<-- inputPWD");
				if (userID.equals(id)){
					if (userPWD.equals(pwd)){
							mainDisplay = new MainDisplay();
							mainDisplay.setVisible(true);
							this.rsClose();
							this.stmtClose();
							this.dbClose();
							this.dispose();
					} else {
						JOptionPane.showMessageDialog(jfr, "비밀번호가 틀렸습니다.", "경고", JOptionPane.QUESTION_MESSAGE);
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(jfr, "해당 ID가 존재하지 않습니다.", "경고", JOptionPane.QUESTION_MESSAGE);
		}
	}
	
	public void dbClose() throws SQLException {
		this.conn.close();
	}
	
	public void stmtClose() throws SQLException {
		this.stmt.close();
	}
	
	public void rsClose() throws SQLException {
		this.rs.close();
	}
	
	public static void main(String[] args){
		new LoginDisplay();
	}
}